<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta name="renderer" content="webkit"/>
    <meta name="force-rendering" content="webkit"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <meta charset="UTF-8"/>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0, viewport-fit=cover"/>
    <title>智能助理</title>
    <link rel="stylesheet" href="//g.alicdn.com/chatui/sdk-v2/0.3.5/sdk.css">
</head>
<body>
<div id="root"></div>
<script src="//g.alicdn.com/chatui/sdk-v2/0.3.5/sdk.js"></script>
<script src="//g.alicdn.com/chatui/extensions/5.15.3/isv-parser.js"></script>
<script>
    var params = location.search.substring(1).split('&')
    var paramMap = {}
    for (let p of params) {
        let kv = p.split('=')
        paramMap[kv[0]] = decodeURI(kv[1])
    }
    var bot = new ChatSDK({
        config: {
            navbar: {
                title: paramMap.name
            },
            robot: {
                avatar: '//gw.alicdn.com/tfs/TB1U7FBiAT2gK0jSZPcXXcKkpXa-108-108.jpg'
            },
            messages: [
                {
                    type: 'text',
                    content: {
                        text: paramMap.name + '您服务，请问有什么可以帮您？'
                    }
                }
            ]
        },
        requests: {
            send: function (msg) {
                if (msg.type === 'text') {
                    return fetch('/ai/v1/chat/completions',{
                        method: 'POST',
                        headers: {
                            token: sessionStorage._token,
                            Authorization: 'Bearer ' + paramMap.app,
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({
                            messages:[{
                                role: 'user',
                                content: msg.content.text
                            }]
                        }),
                    }).then(function (r){
                        return r.text();
                    }).then(function (r){
                        console.log(r)
                       return {
                           _id:Date.now()+"",
                            type:'text',
                            content:{
                               text:r
                            }
                       }
                    })
                }
            }
        }
    });

    bot.run();
</script>
<script src="//g.alicdn.com/chatui/icons/2.0.2/index.js" async></script>
</body>
</html>
